/**
 * 
 */
package com.ms.game.common.container;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

import com.ms.game.invitefriend.bin.InviteCodeType;
import com.xpec.c4.db.dao.PlayerLevelCalculator;
import com.xpec.c4.game.common.Helper;

/**
 * 從 {@link LevelInfoBin} 取資料的工具
 * 
 * @author TW15010
 */
public class LevelInfoBinUtil extends BasicBinUtil {
	private static LevelInfoBin bin = null;

	/**
	 * 玩家等級計算機
	 */
	private static PlayerLevelCalculator levelCalculator;
	
	@Override
	public void onBinLoad(Object binInstance) {
		bin = (LevelInfoBin) binInstance;
		
		initLevelCalculator(bin);
		
		return;
	}

	/**
	 * 取得指定等級對應的抽佣率資料
	 * 
	 * @param level
	 *            角色會員等級
	 * @return 抽佣率資料
	 */
	public static CommissionRateInfo getCommissionRateInfo(int level,InviteCodeType type) {
		ArrayList<CommissionRateInfo> coList = bin.getCommissionRateInfoMap().get(type);

		for (CommissionRateInfo co : coList) {
			if (level >= co.getLvBeg() && level <= co.getLvEnd()) {
				return co;
			}
		}

		return null;
	}

	/**
	 * 取得指定等級對應的抽佣率
	 * 
	 * @param level
	 *            角色會員等級
	 * @return 抽佣率
	 */
	public static float getCommissionRate(int level , InviteCodeType type) {
		CommissionRateInfo info = getCommissionRateInfo(level, type);
		if (info == null) {
			return 0f;
		}

		return info.getComRate();
	}
	
	/**
	 * 取得指定等級的升級資訊
	 */
	public static LevelUpInfo getLevelUpInfoByPlayerLevel(int playerLevel) {
		ArrayList<LevelUpInfo> vipInfoList = bin.getVipLevelUpInfoList();

		LevelUpInfo info = null;
		for(LevelUpInfo vv:vipInfoList)
		{
			if( playerLevel >= vv.getPlayerlv())
			{
				info = vv;
			}
		}
		return info;
	}
	
	/**
	 * 初始化等級計算機
	 */
	private static void initLevelCalculator(LevelInfoBin bin){
		levelCalculator = new PlayerLevelCalculator();
		levelCalculator.initFromBin(bin.getLowLevelUpNeedBet(), bin.getLevelUpNeedBet());
	}
	
	/**
	 * 取得當前經驗相關資訊
	 */
	public static HashMap<String, Long> getExpInfos(BigDecimal currentTotalExp){
		return levelCalculator.getInfosByTotalExp(currentTotalExp);
	}
	
	/**
	 * 取得beforeLevel升到(beforeLevel+changeLevel)所需的經驗值
	 */
	public static BigDecimal getExpByLevel(long beforeLevel, long changeLevel){
		Long beforeTotalExp = levelCalculator.getExpByLevel(beforeLevel);
		Long afterTotalExp = levelCalculator.getExpByLevel((beforeLevel + Math.abs(changeLevel)));
		return Helper.sub(afterTotalExp, beforeTotalExp);
	}
}
